// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter/material.dart';
import 'package:hxdj/message/rongcloud/rc_chat/widget/rc_chat_message_widget.dart';
import 'package:hxdj/message/rongcloud/rc_chat/model/rc_chat_model.dart';
import 'package:hxdj/message/rongcloud/chat/rc_chat_history_message_list.dart';
import 'package:hxdj/message/rongcloud/chat/rc_chat_separate_view_model.dart';
import 'package:provider/provider.dart';

enum LoadingPlace {
  none,
  top,
  bottom,
}

class TIMUIKitHistoryMessageListContainer extends StatefulWidget {
  final Widget Function(BuildContext, ChatMessageBaseModel?)? itemBuilder;
  final String conversationID;

  const TIMUIKitHistoryMessageListContainer({
    super.key,
    this.itemBuilder,
    required this.conversationID,
  });

  @override
  State<StatefulWidget> createState() => _TIMUIKitHistoryMessageListContainerState();
}

class _TIMUIKitHistoryMessageListContainerState extends State<StatefulWidget> {
  Future<void> requestForData(int? lastMsgID, RCChatSeparateViewModel model, [int? count]) async {
    //获取原来的数据, 然后 手动添加下数据

    if (model.haveMoreData) {
      await model.getMessages(timestamp: lastMsgID!);
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final RCChatSeparateViewModel separateViewModel = Provider.of<RCChatSeparateViewModel>(context, listen: false);

    return TIMUIKitHistoryMessageListSelector(
      conversationID: separateViewModel.targetId,
      builder: (context, messageList, child) {
        return TIMUIKitHistoryMessageList(
          model: separateViewModel,
          itemBuilder: (context, messageModel) {
            return LayoutBuilder(
              builder: (context, constraints) => ChatMessageBaseWidget(messageModel: messageModel!),
            );
          },
          messageList: messageList,
          onLoadMore: (int? a) async {
            return await requestForData(a, separateViewModel);
          },
        );
      },
    );
  }
}
